# This is a sample Python script.
import json
import time
from datetime import datetime

import requests
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 获取链接池、ORM表对象


# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
from selenium import webdriver
from selenium.webdriver.common.by import By

from geetest.geetest_v3 import GeeTest3

# Chrome浏览器
driver = webdriver.Chrome()
global responseJson


class Product():
  productUrl = None
  productName = None
  productPrice = None
  productImage = None
  nacionalName = None
  sku = None
  spu = None
  nearSaleCount = None
  rank = None
  rankType = None

  def __init__(self, productUrl, productName, productPrice, productImage,
      nacionalName, nearSaleCount, sku, spu, rank, rankType):
    self.productUrl = productUrl
    self.productName = productName
    self.productPrice = productPrice
    self.productImage = productImage
    self.nacionalName = nacionalName
    self.nearSaleCount = nearSaleCount
    self.sku = sku
    self.spu = spu
    self.rank = rank
    self.rankType = rankType


def print_hi(name):
  # Use a breakpoint in the code line below to debug your script.
  print(f'Hi, {name}')  #
  # 最大化
  driver.maximize_window()
  # 页面缩小
  print('[info] 开始请求-获取任务接口')
  response = requests.get('http://localhost:6253/task/getTask')
  print('[info] 获取任务接口请求结束')
  print('[info] 返回值:' + str(response.json()))
  responseJson = response.json()
  with open('temp.txt', 'r', encoding='utf-8') as f:
    text = f.read()
    print("[从历史]" + text)
    if text != '':
      driver.get(text)
    else:
      print('[info] 历史获取为空 ')
      if responseJson['data']['url'] == None:
        print('No tasks')
        time.sleep(60)
        pass
      driver.get(responseJson['data']['url'])
  # zoom_out = "document.body.style.zoom='0.7'"
  # driver.execute_script(zoom_out)
  try:
    check(driver)
  except:
    pass
  try:
    WebDriverWait(driver, 100).until(
        EC.presence_of_element_located(
            (By.CSS_SELECTOR, '.product-list__item-new'))
    )
  except:
    driver.refresh()
  headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
  }
  print('[info] 请求任务开始接口')
  param = {'id': responseJson['data']['id']}
  print('[info] 参数:' + str(param))
  # 启动任务
  startResponse = requests.post('http://localhost:6253/task/start', json=param,
                                headers=headers)
  print('[info] 请求任务开始接口-结束')
  print('[info] 接口返回:' + str(startResponse))

  list = driver.find_elements(By.CSS_SELECTOR, '.product-list__item-new')
  print('list', list)
  # 滚动至元素可见，这个经常用
  page = 0
  while list.__len__() != 0:
    page = page + 1
    print('当前正在爬取' + str(page) + "页")
    print('当前正在爬取' + driver.current_url)
    with open("temp.txt", "w") as f:
      f.write(driver.current_url)

    # 打印抓取的元素
    print('当前list为:', list)
    # zoom_out = "document.body.style.zoom='0.7'"
    # driver.execute_script(zoom_out)
    # 缓慢下滑
    scroll_down(driver)
    productList = []
    for e in list:
      # 滑动到页面的特定位置，例如滑动到页面的底部
      driver.execute_script(
          'document.querySelectorAll(\'.she-close\')[0].click()')
      driver.execute_script(
          'document.querySelectorAll(\'.dialog-top__close-btn\')[0]?.click()')

      try:
        productLink = e.find_element(By.CSS_SELECTOR, '.goods-title-link--jump')

        productImageElement = e.find_element(By.CSS_SELECTOR,
                                             '.crop-image-container')

        # 产品链接
        productUrl = productLink.get_attribute('href')

        productName = productLink.get_attribute('aria-label')

        sku = productLink.get_attribute('data-sku')
        print('sku', sku)
        if sku == None:
          # 有异常 重来
          print_hi("Hi")
          return

        spu = productLink.get_attribute('data-spu')

        # 产品名称
        if productName == '':
          print('No product')
        productName = productName[:500]

        # 产品价格
        productPriceElement = e.find_element(By.CSS_SELECTOR,
                                             '.normal-price-ctn__sale-price')
        productPrice = productPriceElement.text
        if productPrice == None:
          print('[error] 找不到产品价格')
          # 重来
          print_hi("Hi")
          return

        # 产品图片
        productImage = productImageElement.get_attribute('data-before-crop-src')

        # 是否是本土
        nacionalElement = e.find_elements(By.CSS_SELECTOR, '.tag-QuickShip')
        nacionalName = 'Internacional'
        if len(nacionalElement) > 0:
          nacionalName = nacionalElement[0].text

        # 最近售出
        nearSaleCount = None
        nearSaleCountElement = e.find_elements(By.CSS_SELECTOR,
                                               '.product-card__selling-proposition-text')
        if len(nearSaleCountElement) > 0:
          nearSaleCount = nearSaleCountElement[0].text
        if nearSaleCount is not None:
          if "vendidos" not in nearSaleCount:
            # 说明没取出来
            nearSaleCountElement = e.find_elements('.product-card__sales-label')
            if len(nearSaleCountElement) > 0:
              nearSaleCount = nearSaleCountElement[0].text
        if nearSaleCount is None:
          # 还是空 设置为0
          nearSaleCount = 0
        # 产品排名
        productRank = ''
        productRankElement = e.find_elements(By.CSS_SELECTOR,
                                             '.rank-title')
        if len(productRankElement) > 0:
          productRank = productRankElement[0].text

        # 产品排名类型
        productRankTypeElement = e.find_elements(By.CSS_SELECTOR,
                                                 '.rank-sub')
        productRankType = ''
        if len(productRankTypeElement) > 0:
          productRankType = productRankTypeElement[0].text
      except:
        pass

      product = Product(productUrl, productName, productPrice, productImage,
                        nacionalName, nearSaleCount, sku, spu,
                        productRank, productRankType)
      productList.append(product.__dict__)

    # 发送请求
    url = 'http://localhost:6253/batchSaveOrUpdate'
    headers = {
      'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36'
    }
    print(productList)
    jsonStr = json.dumps(productList)
    print(jsonStr)

    # 直接给post方法中的json参数赋值，赋json类型的数据
    response = requests.post(url=url, headers=headers, json=productList)
    page_text = response.text

    print(page_text)
    list = []

    # 判断有没有下一页 有的话继续
    nextButtonElement = driver.find_elements(By.CSS_SELECTOR,
                                             '.sui-pagination__next')
    if len(nextButtonElement) != 0 and (
        nextButtonElement[0].get_attribute('disabled') != 'true'):
      # nextButtonElement[0].click()
      driver.execute_script(
          'document.querySelectorAll(\'.sui-pagination__next\')[0]?.click()')
      try:
        check(driver)
      except:
        pass

      try:
        WebDriverWait(driver, 3).until(
            EC.presence_of_element_located(
                (By.CSS_SELECTOR, '.product-list__item-new'))
        )
      except:
        driver.refresh()
      list = driver.find_elements(By.CSS_SELECTOR, '.product-list__item-new')
    else:
      print('全部爬取完毕')
      with open("temp.txt", "w") as f:
        f.write("")
      print('[info] 请求任务结束接口')
      print('[info] 参数:', param)
      # 启动任务
      startResponse = requests.post('http://localhost:6253/task/incrementCount',
                                    json=param, headers=headers)
      print('[info] 请求任务结束接口')
      print('[info] 接口返回:' + str(startResponse))

  print('当前爬取了' + str(page) + "页")
  print('当前正在爬取' + driver.current_url)


def check(driver):
  # 校验是否有验证码
  WebDriverWait(driver, 1).until(
      EC.visibility_of_element_located(
          (By.CSS_SELECTOR, '.geetest_canvas_bg'))
  )
  geetest = GeeTest3()
  flag = geetest.run(driver)

  if flag:
    # 验证通过
    print('验证通过')
  else:
    print('验证失败')
    print_hi("Hello")


def scroll_down(driver):
  # 执行这段代码，会获取到当前窗口总高度
  js = "return action=document.body.scrollHeight"
  # 初始化现在滚动条所在高度为0
  height = 0
  # 当前窗口总高度
  new_height = driver.execute_script(js)

  # 向下
  while height < new_height:
    # 将滚动条调整至页面底部
    for i in range(height, new_height, 300):
      driver.execute_script('window.scrollTo(0, {})'.format(i))
      time.sleep(0.5)
    height = new_height
    time.sleep(1)
    new_height = driver.execute_script(js)

  #  向上
  for i in range(0, new_height, 300):
    driver.execute_script('window.scrollTo(0, {})'.format(new_height - i))
    time.sleep(0.5)

  while height < new_height:
    # 将滚动条调整至页面底部
    for i in range(height, new_height, 300):
      driver.execute_script('window.scrollTo(0, {})'.format(i))
      time.sleep(0.5)
    height = new_height
    time.sleep(1)
    new_height = driver.execute_script(js)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
  while True:
    try:
      print_hi('PyCharm')
    except Exception as e:
      print(e)
      pass

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
